<?php
/**
 * ACL AuthControlLayer 权限控制层
 *
 * @author seraph.liujb 2009.8.14
 */

class Common_ACL
{
	/**
	 * 服务地址
	 * @var string
	 */
	protected $_url = 'http://192.168.10.254:8080/Service/Account';

	/**
	 * 构造方法
	 */
	public function __construct()
	{
	}

	/**
	 * 执行请求
	 * @param $xml
	 * @param $method
	 * @return array
	 */
	private function _doRequest($xml, $method = Zend_Http_Client::POST)
	{
		$client = new Zend_Http_Client($this->_url);
		$client->setMethod($method);
		$client->setRawData($xml);
		$response = $client->request();
		$result = Zend_Json::decode(Zend_Json::fromXml($response->getBody()));
		return $result['body'];
	}
	
	/**
	 * 校验请求的角色资源授权是否有效
	 * @param $roleId 角色id
	 * @param $resoId 资源id
	 * @param $access 授权操作码
	 * @return boolean
	 */
	public function verify($roleId, $resoId, $access)
	{
		//生成XML
		$xml  = '<body controller="acl" action="verify" >';
		$xml .= '<id>' .$resoId. '</id>';
		$xml .= '<access>' .$access. '</access>';
		$xml .= '<role>' .$roleId. '</role>';
		$xml .= '</body>';
		//
		return $this->_doRequest($xml);
	}
	
	/**
	 * 新建
	 * @param $roleId 角色id
	 * @param $resoId 资源id
	 * @param $access 授权操作码
	 * @return array
	 */
	public function create($roleId, $resoId, $access)
	{
		//生成XML
		$xml  = '<body controller="acl" action="create" >';
		$xml .= '<id>' .$resoId. '</id>';
		$xml .= '<access>' .$access. '</access>';
		$xml .= '<role>' .$roleId. '</role>';
		$xml .= '<parent></parent>';
		$xml .= '</body>';
		//
		return $this->_doRequest($xml);
	}
	
	/**
	 * 修改
	 * @param $roleId 角色id
	 * @param $resoId 资源id
	 * @param $access 授权操作码
	 * @return array
	 */
	public function update($roleId, $resoId, $access)
	{
		return $this->create($roleId, $resoId, $access);
	}
	
	/**
	 * 删除
	 * @param $roleId 角色id
	 * @param $resoId 资源id
	 * @return array
	 */
	public function delete($roleId, $resoId, $access = '')
	{
		//生成XML
		$xml  = '<body controller="acl" action="delete" >';
		$xml .= '<id>' .$resoId. '</id>';
		$xml .= '<access>' .$access. '</access>';
		$xml .= '<role>' .$roleId. '</role>';
		$xml .= '</body>';
		//
		return $this->_doRequest($xml);
	}
}
